package com.chen.controller;

import com.chen.domain.Coin;
import com.chen.domain.CoinConfig;
import com.chen.dto.R;
import com.chen.service.CoinConfigService;
import com.chen.service.CoinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: chen
 * @Date: 2021/5/11 11:21 AM
 * @Version 1.0
 */
@RestController
@RequestMapping("/coinConfigs")
@Api(tags = "币种配置的控制器")
public class CoinConfigController {

    @Autowired
    private CoinConfigService coinConfigService;

    @Autowired
    private CoinService coinService;


    @GetMapping("/info/{coinId}")
    @ApiOperation(value = "查询币种的配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinId" ,value = "币种的id值")
    })
    public R<CoinConfig> getCoinConfig(@PathVariable("coinId") Long coinId){
        CoinConfig coinConfig =  coinConfigService.getById(coinId) ;
        return R.ok(coinConfig) ;
    }

    @PatchMapping
    @ApiOperation(value = "币种配置的修改操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinConfig" ,value ="coinConfig的json数据" )
    })
    public R  update(@RequestBody  @Validated CoinConfig coinConfig){

        Coin coin = coinService.getById(coinConfig.getId());
        coinConfig.setName(coin.getName());
        coinConfig.setCoinType(coin.getType());
        boolean saveOrUpdate  =  coinConfigService.saveOrUpdate(coinConfig) ;
        if(saveOrUpdate){
            return R.ok() ;
        }
        return R.fail("修改失败") ;
    }

}
